import { ElMessageBox, ElMessageBoxOptions } from 'element-plus';
import { VNode } from 'vue';
import './index.scss';

export const MciMessageBox = {
  async alert(message: string, title: string, options?: ElMessageBoxOptions) {
    await ElMessageBox.alert(message, title, options);
  },
  async confirm(
    message: string | VNode | (() => VNode),
    title?: string,
    options?: ElMessageBoxOptions & { requestLoading?: () => Promise<any> }
  ) {
    // 配置后，默认截断直接关闭消息框，在接口请求返回后执行关闭
    if (options?.requestLoading) {
      options.beforeClose = (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true;

          options
            ?.requestLoading?.()
            .then(() => {
              done();
              instance.confirmButtonLoading = false;
            })
            .catch(() => {
              instance.confirmButtonLoading = false;
            });
        } else {
          if (instance.confirmButtonLoading) return;
          done();
        }
      };
    }

    await ElMessageBox.confirm(message, title, options);
  },
  async prompt(message: string, title: string, options?: ElMessageBoxOptions) {
    await ElMessageBox.prompt(message, title, options);
  },
  close() {
    ElMessageBox.close();
  }
};
